package com.ruoyi.sso.controller;

import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections4.map.HashedMap;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.sso.util.FileUtil;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;

/**
 * commonController
 * 
 * @author zsj
 * @date 2019-12-24
 */
@Controller
@RequestMapping("/sso/common")
public class SsoCommonController extends BaseController {

	@GetMapping("/weather")
	@ResponseBody
	public String weather(HttpServletRequest request) {
		String city = request.getParameter("city");
		return HttpUtil.get("https://www.mxnzp.com/api/weather/current/" + city);
	}

	/**
	 * 代理转发
	 * 
	 * @param url    转发地址
	 * @param params 参数，json格式化
	 * @param type   请求类型get/post
	 * @return
	 */
	@PostMapping("/proxyToWy")
	@ResponseBody
	public String proxyToWy(HttpServletRequest request) {
		String url = request.getParameter("url");
		String params = request.getParameter("params");
		String type = request.getParameter("type");

		if (StrUtil.isNotBlank(type)) {
			if ("get".equals(type.toLowerCase())) {

				return HttpUtil.get(url);
			} else if ("post".equals(type.toLowerCase())) {

				return HttpUtil.post(url, JSON.parseObject(params));
			}
		}
		Map<String, Object> map = new HashedMap<String, Object>();
		map.put("msg", "请求类型异常:post/get");
		map.put("code", 1);
		return JSON.toJSONString(map);

	}

	/**
	 * url 文件代理中转
	 * 
	 * @param response
	 * @param fileName
	 * @param url
	 * @throws IOException
	 */
	@RequestMapping("/downloadToWy")
	public void proxyFile(HttpServletResponse response, HttpServletRequest request  )
			throws IOException {
		String fileUrl = request.getParameter("fileUrl");
		FileUtil.proxyUrlFile(response, fileUrl, null, null);

	}

}
